package com.efruit.struts.action;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.LookupDispatchAction;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.efruit.dao.impl.OrderDAO;
import com.efruit.po.Order;
import com.efruit.struts.form.CheckoutForm;
import com.efruit.to.AuthorityTO;

public class CheckoutAction extends LookupDispatchAction {

	@Override
	protected Map getKeyMethodMap() {
		Map map = new HashMap();
		map.put("checkout.makeorder", "makeorder");
		map.put("checkout.cancel", "cancel");
		return map;
	}
	public static void main(String args[]){
		System.out.println(new Date().toString().length());
	}

	public ActionForward makeorder(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		CheckoutForm checkoutForm = (CheckoutForm)form;
		
		Order order = new Order((Float) request.getSession().getAttribute("total"),
				new Date().toString(), 0, checkoutForm.getAddress(), ((AuthorityTO) request
						.getSession().getAttribute("authorityTo")).getId());
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(
				"applicationContext.xml");
		OrderDAO dao = (OrderDAO) ctx.getBean("OrderDAO");
		try {
			dao.save(order);
		} catch (Exception e) {
			e.printStackTrace();
			return mapping.findForward("error");
		}
		return mapping.findForward("ordermade");
	}

	public ActionForward cancel(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {

		return mapping.findForward("shopcart");
	}

}
